package utils;

import play.db.jpa.JPA;

import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class MetaModel {


    public static List<Class> getEntities() {
        List<Class> entities = new ArrayList<Class>();
        if (JPA.isEnabled()) {
            Metamodel metamodel = JPA.em().getMetamodel();
            Set<EntityType<?>> entityTypes = metamodel.getEntities();
            Iterator entityTypesIt = entityTypes.iterator();
            while (entityTypesIt.hasNext()) {
                EntityType entityType = (EntityType) entityTypesIt.next();
                Class entity = entityType.getJavaType();
                entities.add(entity);
            }
        }
        return entities;
    }
}
